﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Threading;
using System.Xml.Linq;

namespace Jukebox
{
    /// <summary>
    /// The primary interface for the jukebox, use the JukeboxInstance instance method to access the instance
    /// You should use this interface to control the jukebox, to extend the jukebox you can modify the SongData and CommercialData structures
    /// </summary>
    public interface IJukebox
    {
        void Stop();
        Guid? AddSong(SongData song);
        void RemoveSong(Guid song);
        void AddCommercial(CommercialData commercial);
        void CancelCommercial();
        XElement GetPlaylist();
    }
    /// <summary>
    /// The Jukebox instance implementation
    /// This class is a singleton
    /// </summary>
    public class JukeboxInstance : IJukebox
    {
        static JukeboxInstance instance = new JukeboxInstance();
        /// <summary>
        /// Provides access to the jukebox instance
        /// </summary>
        public static IJukebox Instance
        {
            get
            {
                return instance;
            }
            
        }
        Thread mThread = null;
        State mState;
        Semaphore mSemaphore = new Semaphore(0, 1000);
        Queue<Message> mMessages = new Queue<Message>();
        JukeboxInstance()
        {
            mState = new Idle(this);
            mThread = new Thread(this.Run);
            mThread.Start();
        }
        /// <summary>
        /// Internal method
        /// Used to change state in the state machine
        /// </summary>
        /// <param name="nextState">An instance of the next state to be active</param>
        internal void ChangeState(State nextState)
        {
            mState = nextState;
        }

        /// <summary>
        /// This is the thread entry method
        /// </summary>
        void Run()
        {
            while (mState.Update())
            {
                if (mSemaphore.WaitOne(mState.GetTimeout()))
                {
                    Message msg = null;
                    lock (this)
                    {
                        msg = mMessages.Dequeue();
                    }
                    msg.Execute(mState);
                }
            }
        }
        /// <summary>
        /// Send a stop message to the state machine
        /// This will cause the thread to exit
        /// </summary>
        public void Stop()
        {
            lock (this)
            {
                mMessages.Enqueue(new Stop());
                mSemaphore.Release();
            }
        }
        /// <summary>
        /// 
        /// </summary>
        /// <param name="song">Information about the song to be played</param>
        /// <returns>
        /// A globally unique ID to identify the play position in the playlist.
        /// If the method returns null then the song was not added to the playlist.
        /// </returns>
        public Guid? AddSong(SongData song)
        {
            AddSong msg = new AddSong(song);
            lock (this)
            {
                mMessages.Enqueue(msg);
                mSemaphore.Release();
            }
            return msg.WaitForResult();
        }
        /// <summary>
        /// Remove a song from the playlist using the Guid returned from AddSong
        /// </summary>
        /// <param name="song">A Guid returned from AddSong</param>
        public void RemoveSong(Guid song)
        {
            lock (this)
            {
                mMessages.Enqueue(new RemoveSong(song));
                mSemaphore.Release();
            }
        }
        /// <summary>
        /// Add a commercial to the state machine
        /// </summary>
        /// <param name="commercial">Information about the commercial</param>
        public void AddCommercial(CommercialData commercial)
        {
            lock (this)
            {
                mMessages.Enqueue(new AddCommercial(commercial));
                mSemaphore.Release();
            }
        }
        /// <summary>
        /// Cancel any running commercial
        /// </summary>
        public void CancelCommercial()
        {
            lock (this)
            {
            mMessages.Enqueue(new CancelCommercial());
            mSemaphore.Release();
            }
        }
        /// <summary>
        /// Obtain a XML node that represents the current playlist
        /// </summary>
        /// <returns></returns>
        public XElement GetPlaylist()
        {
            GetPlaylist msg = new GetPlaylist();
            lock (this)
            {
                mMessages.Enqueue(msg);
                mSemaphore.Release();
            }
            return msg.WaitForResult();
        }

    }
}